###########################################
#  Primary Divisions: How Voters Evaluate Policy and Group Differences in Intra-Party Contests
#   - Forthcoming at The Journal of Politics
#   - Henderson et al 2021
#
###########################################
#  - code by S. Goggin & J. Henderson
########################################################
# This file measures candidates support by agreement x consistency for levels of knowledege and crosspressure, by sophistication levels
########################################################

#dirs="~/Dropbox/replication0/"
#dirs should be set here or in runR.R 

rm(list=ls()[which(ls()!='dirs')])
library(ggplot2)
library(stringr)

# messy function to reorder by some estimate value
lableOrder=function(xmat,labels,label.groups,omits,o.column){

	# denote which label is to be omitted on the label
	for(i in 1:length(omits)){
		labels[which(labels==omits[i])]=paste('omit',labels[which(labels==omits[i])],sep='_')
	}

	# break groups into levels
	un_group=unique(label.groups)

	# this is the item to sort on, typically global or independent
	xm=xmat[,o.column]

	# vector which will contain row order
	xo=1:length(xm)

	# rearranging roworder within level
	for(j in 1:length(un_group)){
		ix=which(label.groups==un_group[j])
		if(length(ix)>2){
			ix=ix[!grepl(labels[ix],pattern='omit')]
			xo[ix]=xo[ix][order(xm[ix])]
		}
	}
	return(xmat[xo,])
}

reOrder=function(x,o){
	ix=array(NA,nrow(x))
	for(i in 1:length(o)){
		ix[i]=which(x$iv_order==o[i])
	}
	return(x[ix,])
}

setwd(dirs)

load('data/data_matrix_scored.Rdata')
scored=read.csv('data/candidate_matrix.csv')


###########################################
###First, need to stack based on candidates, not just candidate pairs (and also get text out for labels later)

#This has leaners as independents, which is incorrect
#cces_stacked$pid3clean <- ifelse(cces_stacked$pid3=="Democrat",-1,ifelse(cces_stacked$pid3=="Republican",1,0))

library(car)

###########################################
###Then, produce models w/ Clustered SEs

#Function from: http://scholar.byu.edu/jgubler/book/clustered-standard-errors-r
#Need this for clustered standard errors
clse.f <- function(dat,fm, cluster){
 require(sandwich)
 require(lmtest)
 not <- attr(fm$model,"na.action")
if( ! is.null(not)){
  cluster <- cluster[-not]
    dat <- dat[-not,]
}
 with(dat,{
 M <- length(unique(cluster))
 N <- length(cluster)
 K <- fm$rank
 dfc <- (M/(M-1))*((N-1)/(N-K))
 uj <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
 vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
 coeftest(fm, vcovCL)
 }
 )
}

data_matrix=data_matrix[,-c(grep(names(data_matrix),pattern='scores'))]
candidate_matrix=cbind(data_matrix,scored[,c('scores','scores_var','scores_policy','scores_policy_var')])

# i_policy is conjoint position in either i1 or i2
# consistency here is candidate too consistent Dem or Rep position on policy
# analogous to above variance in aggregating over candidate profiles on issues

candidate_matrix$consistency=
as.numeric(candidate_matrix$"i_lgbt"==1)*-1+
as.numeric(candidate_matrix$"i_marriage"==1)*1+
as.numeric(candidate_matrix$"i_need"==1)*-1+
as.numeric(candidate_matrix$"i_govabuse"==1)*1+
as.numeric(candidate_matrix$"i_freetrade"==1)*-1+
as.numeric(candidate_matrix$"i_unfairtrade"==1)*1+
as.numeric(candidate_matrix$"i_righttochoose"==1)*-1+
as.numeric(candidate_matrix$"i_unbornlives"==1)*1+
as.numeric(candidate_matrix$"i_raisetaxes"==1)*-1+
as.numeric(candidate_matrix$"i_cuttaxes"==1)*1+
as.numeric(candidate_matrix$"i_co2emissions"==1)*-1+
as.numeric(candidate_matrix$"i_drilling"==1)*1+
as.numeric(candidate_matrix$"i_citizenship"==1)*-1+
as.numeric(candidate_matrix$"i_bordersecurity"==1)*1+
as.numeric(candidate_matrix$"i_guncontrol"==1)*-1+
as.numeric(candidate_matrix$"i_gunrights"==1)*1+
as.numeric(candidate_matrix$"i_reducemilitary"==1)*-1+
as.numeric(candidate_matrix$"i_strengthenmilitary"==1)*1+
as.numeric(candidate_matrix$"i_policing"==1)*-1+
as.numeric(candidate_matrix$"i_criminals"==1)*1


candidate_matrix$libcon=rowMeans(cbind(candidate_matrix[,grep(names(candidate_matrix),pattern='libcon')],candidate_matrix$self_place/3))


############################################################
############################################################
###Now for Primary Elections (by PID)  -  InParty
############################################################
############################################################
###Now for Primary Elections (by PID)  -  OutParty

load('data/issueCrossPressureData.Rdata')

########################################################################
########################################################################
# matrix of two-way support by issue 1 and issue 2 by party
########################################################################
########################################################################

reps_agree=dems_agree=as.data.frame(matrix(NA,20,20))
ixa=c("need","govabuse",
"reducemilitary","strengthenmilitary",
"raisetaxes","cuttaxes",
"lgbt","marriage",
"co2emissions","drilling",
"freetrade","unfairtrade",
"righttochoose","unbornlives",
"guncontrol","gunrights",
"citizenship","bordersecurity",
"policing","criminals")

#ixb=c('libcon_need','libcon_dfns','libcon_taxs','libcon_gays','libcon_envs',
	#'libcon_trad','libcon_abrt','libcon_guns','libcon_immi','libcon_crme')


issue_mat=cbind(
		as.numeric(candidate_matrix$libcon_need<=0),
		as.numeric(candidate_matrix$libcon_need>=0),
		as.numeric(candidate_matrix$libcon_dfns<=0),
		as.numeric(candidate_matrix$libcon_dfns>=0),
		as.numeric(candidate_matrix$libcon_taxs<=0),
		as.numeric(candidate_matrix$libcon_taxs>=0),
		as.numeric(candidate_matrix$libcon_gays<=0),
		as.numeric(candidate_matrix$libcon_gays>=0),
		as.numeric(candidate_matrix$libcon_envs<=0),
		as.numeric(candidate_matrix$libcon_envs>=0),
		as.numeric(candidate_matrix$libcon_trad<=0),
		as.numeric(candidate_matrix$libcon_trad>=0),
		as.numeric(candidate_matrix$libcon_abrt<=0),
		as.numeric(candidate_matrix$libcon_abrt>=0),
		as.numeric(candidate_matrix$libcon_guns<=0),
		as.numeric(candidate_matrix$libcon_guns>=0),
		as.numeric(candidate_matrix$libcon_immi<=0),
		as.numeric(candidate_matrix$libcon_immi>=0),
		as.numeric(candidate_matrix$libcon_crme<=0),
		as.numeric(candidate_matrix$libcon_crme>=0)
)
colnames(issue_mat)=ixa
rownames(reps_agree)=rownames(dems_agree)=colnames(reps_agree)=colnames(dems_agree)=ixa


for(i in 1:20){
	for(j in i:20){
		dems_agree[i,j]=mean(issue_mat[which(candidate_matrix$pid==-1),i]==1 & issue_mat[which(candidate_matrix$pid==-1),j]==1)
		reps_agree[i,j]=mean(issue_mat[which(candidate_matrix$pid== 1),i]==1 & issue_mat[which(candidate_matrix$pid== 1),j]==1)
	}
}

############################################################
# baseline agreement measures of issue-by-issue ideology
############################################################

library(xtable)
#print(xtable(dems_agree),file='appendix/figures/demsIssueAgreeMatrix.tex')
#print(xtable(reps_agree),file='appendix/figures/repsIssueAgreeMatrix.tex')

load('data/sophistication_indices.Rdata')
L_ix2=indices$resp_id[indices$"L_ix2"]
M_ix2=indices$resp_id[indices$"M_ix2"]
H_ix2=indices$resp_id[indices$"H_ix2"]

############################################################
# inconsistency w/n primary and outparty comparisons
############################################################
# invote
invote$sophistication=NA
#invote$M_ix2=NA
#invote$L_ix2=NA

for(i in 1:length(H_ix2)){
	iq=which(invote$respondent==H_ix2[i])
	if(length(iq)>0){
		invote$sophistication[iq]='H'
	}
}

for(i in 1:length(M_ix2)){
	iq=which(invote$respondent==M_ix2[i])
	if(length(iq)>0){
		invote$sophistication[iq]='M'
	}
}

for(i in 1:length(L_ix2)){
	iq=which(invote$respondent==L_ix2[i])
	if(length(iq)>0){
		invote$sophistication[iq]='L'
	}
}


# outvote
outvote$sophistication=NA
#outvote$M_ix2=NA
#outvote$L_ix2=NA

for(i in 1:length(H_ix2)){
	iq=which(outvote$respondent==H_ix2[i])
	if(length(iq)>0){
		outvote$sophistication[iq]='H'
	}
}

for(i in 1:length(M_ix2)){
	iq=which(outvote$respondent==M_ix2[i])
	if(length(iq)>0){
		outvote$sophistication[iq]='M'
	}
}

for(i in 1:length(L_ix2)){
	iq=which(outvote$respondent==L_ix2[i])
	if(length(iq)>0){
		outvote$sophistication[iq]='L'
	}
}

# H sophistication

reps <- subset(invote, invote$pid3==1 & invote$sophistication=='H')
dems <- subset(invote, invote$pid3==-1 & invote$sophistication=='H')
inds <- subset(invote, invote$pid3==0)

dems$consistency1=as.numeric(dems$consistency1==-1)
dems$consistency2=as.numeric(dems$consistency2==-1)
reps$consistency1=as.numeric(reps$consistency1== 1)
reps$consistency2=as.numeric(reps$consistency2== 1)

inds$consistency1[which(inds$pty==1)]=as.numeric(inds$consistency1[which(inds$pty==1)]== 1)
inds$consistency2[which(inds$pty==1)]=as.numeric(inds$consistency2[which(inds$pty==1)]== 1)
inds$consistency1[which(inds$pty==0)]=as.numeric(inds$consistency1[which(inds$pty==0)]==-1)
inds$consistency2[which(inds$pty==0)]=as.numeric(inds$consistency2[which(inds$pty==0)]==-1)


reps$consistency=sign(reps$consistency)+1
dems$consistency=abs(sign(dems$consistency)-1)

inds$consistency[which(inds$pty==1)]=sign(inds$consistency[which(inds$pty==1)])+1
inds$consistency[which(inds$pty==0)]=abs(sign(inds$consistency[which(inds$pty==0)])-1)

invote=rbind(dems,reps,inds)

############################################################
# inconsistency w/n primary and outparty comparisons
############################################################
# outvote

dems <- subset(outvote, outvote$pid3==1 & outvote$sophistication=='H')
reps <- subset(outvote, outvote$pid3==-1 & outvote$sophistication=='H')
inds <- subset(outvote, outvote$pid3==0)

dems$consistency1=as.numeric(dems$consistency1==-1)
dems$consistency2=as.numeric(dems$consistency2==-1)
reps$consistency1=as.numeric(reps$consistency1== 1)
reps$consistency2=as.numeric(reps$consistency2== 1)

inds$consistency1[which(inds$pty==1)]=as.numeric(inds$consistency1[which(inds$pty==1)]== 1)
inds$consistency2[which(inds$pty==1)]=as.numeric(inds$consistency2[which(inds$pty==1)]== 1)
inds$consistency1[which(inds$pty==0)]=as.numeric(inds$consistency1[which(inds$pty==0)]==-1)
inds$consistency2[which(inds$pty==0)]=as.numeric(inds$consistency2[which(inds$pty==0)]==-1)

reps$consistency=sign(reps$consistency)+1
dems$consistency=abs(sign(dems$consistency)-1)

inds$consistency[which(inds$pty==1)]=sign(inds$consistency[which(inds$pty==1)])+1
inds$consistency[which(inds$pty==0)]=abs(sign(inds$consistency[which(inds$pty==0)])-1)

outvote=rbind(dems,reps,inds)

invote=cbind(invote,'invote')
outvote=cbind(outvote,'outvote')
names(outvote)[ncol(outvote)]=names(invote)[ncol(invote)]='invoted'
invote[,ncol(invote)]=as.character(invote[,ncol(invote)])
outvote[,ncol(outvote)]=as.character(outvote[,ncol(outvote)])

vote=rbind(invote,outvote)

################################################################
# consistency pooling dems and reps
################################################################

ds=quantile(vote$libcon[which(vote$pid==-1)],pr=c(.75,.5))
rs=quantile(vote$libcon[which(vote$pid== 1)],pr=c(.25,.5))

vote$crossp25=NA
vote$crossp50=NA

vote$crossp25[which(vote$pid==-1)]=as.numeric(vote$libcon[which(vote$pid==-1)]>ds[1])
vote$crossp50[which(vote$pid==-1)]=as.numeric(vote$libcon[which(vote$pid==-1)]>ds[2])

vote$crossp25[which(vote$pid== 1)]=as.numeric(vote$libcon[which(vote$pid== 1)]<rs[1])
vote$crossp50[which(vote$pid== 1)]=as.numeric(vote$libcon[which(vote$pid== 1)]<rs[2])

###TBD predprob
#
vote$c0a0=vote$consistency1+vote$consistency2==0 & vote$agree1+vote$agree2==0
vote$c0a1=vote$consistency1+vote$consistency2==0 & vote$agree1+vote$agree2==1
vote$c0a2=vote$consistency1+vote$consistency2==0 & vote$agree1+vote$agree2==2

vote$c1a0=vote$consistency1+vote$consistency2==1 & vote$agree1+vote$agree2==0
vote$c1a2=vote$consistency1+vote$consistency2==1 & vote$agree1+vote$agree2==2

vote$c1a1=vote$consistency1+vote$consistency2==1 & vote$agree1+vote$agree2==1
## does agreement x constituency matter on the
# 1 0 | 0 1
# 0 1 | 1 0

# 1 0 | 1 0
# 0 1 | 0 1
vote$c1a1_align=(vote$consistency1==1 & vote$agree1==1 & vote$consistency2==0 & vote$agree2==0) | (vote$consistency1==0 & vote$agree1==0 & vote$consistency2==1 & vote$agree2==1)
vote$c1a1_misal=(vote$consistency1==0 & vote$agree1==1 & vote$consistency2==1 & vote$agree2==0) | (vote$consistency1==1 & vote$agree1==0 & vote$consistency2==0 & vote$agree2==1)

vote$c2a0=vote$consistency1+vote$consistency2==2 & vote$agree1+vote$agree2==0
vote$c2a1=vote$consistency1+vote$consistency2==2 & vote$agree1+vote$agree2==1
vote$c2a2=vote$consistency1+vote$consistency2==2 & vote$agree1+vote$agree2==2


attach(vote)
pred_prob_invote <- lm(dv_choice~
	#vote$c0a0+
	vote$c0a1+vote$c0a2+
	vote$c1a0+vote$c1a1+vote$c1a2+
	vote$c2a0+vote$c2a1+vote$c2a2,subset=which(vote$invoted=='invote'))
pred_prob_invote_clse <- clse.f(vote[which(vote$invoted=='invote'),],pred_prob_invote,respondent[which(vote$invoted=='invote')])


###
attach(vote)
# fixing 2 consistent positions :: co-party and out-party voters

##########################################
# in party
##########################################

weighted.mean(w=wt[which(pid3!=0 & invoted=='invote' & consistency1==1 & consistency2==1)],dv_choice[which(pid3!=0 & invoted=='invote' & consistency1==1 & consistency2==1)])
#0.599
weighted.mean(w=wt[which(pid3!=0 & invoted=='invote' & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))],dv_choice[which(pid3!=0 & invoted=='invote' & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))])
#0.381
weighted.mean(w=wt[which(pid3!=0 & invoted=='invote' & consistency1==0 & consistency2==0)],dv_choice[which(pid3!=0 & invoted=='invote' & consistency1==0 & consistency2==0)])
#0.188

# consistent positions drop off less among those who disagree w/n party
x1=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)],dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)])
n1=length(dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)])
#0.614
x2=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))],dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))])
n2=length(dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))])
#0.502
x3=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)],dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)])
n3=length(dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)])
#NaN

y1=sum(na.rm=T,(c(n1,n2,n3)/sum(na.rm=T,c(n1,n2,n3,na.rm=T)))*c(x1,x2,x3))


x7=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)],dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)])
n7=length(dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)])
#0.130
x8=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))],dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))])
n8=length(dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))])
#0.260
x9=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)],dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)])
n9=length(dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)])
#0.124

y3=sum((c(n7,n8,n9)/sum(c(n7,n8,n9)))*c(x7,x8,x9))


# happily split :: similar split/cross-pressure
x4=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))],dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))])
n4=length(dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))])
#0.464 #=> deviate from a consistent position in party primary, who expect to replace above loss on one deviation

# merge two-way split
# party consistent defectors
#mean(dv_choice[which(invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))])
#0.401 #=> deviate from a consistent position in party primary, who expect is first to go on one deviation

# moderate splits
#mean(dv_choice[which(invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))])
#0.434 #=> deviate from a consistent position in party primary, who expect is first to go on one deviation

x5=weighted.mean(w=c(wt[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],wt[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]),
c(dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]))
n5=length(c(dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]))
#.350

# unhappily split ::
x6=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))],dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))])
n6=length(dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))])
#0.311 #=> deviate from a consistent position in party invote, who expect to replace above loss on one deviation

y2=sum((c(n4,n5,n6)/sum(c(n4,n5,n6)))*c(x4,x5,x6))


defect_mat=rbind(c(x1,x2,x3),c(x4,x5,x6),c(x7,x8,x9))
rownames(defect_mat)=c('consistent 2','consistent 1','consistent 0')
colnames(defect_mat)=c('agree 2','agree 1','agree 0')
defect_mat=cbind(defect_mat,c(y1,y2,y3))

#effective unit sizes
defect_mat_n=rbind(
	c(
		round((n1/nrow(invote))*length(unique(invote$respondent))),
		round((n2/nrow(invote))*length(unique(invote$respondent))),
		round((n3/nrow(invote))*length(unique(invote$respondent)))
	),
	c(
		round((n4/nrow(invote))*length(unique(invote$respondent))),
		round((n5/nrow(invote))*length(unique(invote$respondent))),
		round((n6/nrow(invote))*length(unique(invote$respondent)))
	),
	c(
		round((n7/nrow(invote))*length(unique(invote$respondent))),
		round((n8/nrow(invote))*length(unique(invote$respondent))),
		round((n9/nrow(invote))*length(unique(invote$respondent)))
	)
)


rownames(defect_mat_n)=c('consistent 2','consistent 1','consistent 0')
colnames(defect_mat_n)=c('agree 2','agree 1','agree 0')
#defect_mat_n=cbind(defect_mat_n,c(y1,y2,y3))
## exploring the outparty conditions

yy1=c(y1,y2,y3)


##########################################
# out party
##########################################
# above is just co-partisans
# below merges out-partisans

#
weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & consistency1==1 & consistency2==1)],dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==1 & consistency2==1)])
#0.352
weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))],dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))])
#0.590
weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & consistency1==0 & consistency2==0)],dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==0 & consistency2==0)])
#0.727

# consistent positions drop off less among those who disagree w/n party
x1=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)],dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)])
n1=length(dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)])
#0.608
x2=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))],dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))])
n2=length(dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))])
#0.424
x3=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)],dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)])
n3=length(dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)])
#0.320

y1=sum((c(n1,n2,n3)/sum(c(n1,n2,n3)))*c(x1,x2,x3))

x7=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)],dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)])
n7=length(dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)])
#0.728
x8=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))],dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))])
n8=length(dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))])
#0.722
x9=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)],dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)])
n9=length(dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)])
#NaN

y3=sum((c(n7,n8,n9)/sum(c(n7,n8,n9)))*c(x7,x8,x9))


# happily split :: similar split/cross-pressure
x4=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))],dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))])
n4=length(dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))])
#0.653 #=> deviate from a consistent position in party invote, who expect to replace above loss on one deviation

# party consistent defectors
#mean(dv_choice[which(invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))])
#0.600 #=> deviate from a consistent position in party invote, who expect is first to go on one deviation

# moderate splits
#mean(dv_choice[which(invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))])
#0.546 #=> deviate from a consistent position in party invote, who expect is first to go on one deviation

x5=weighted.mean(w=c(wt[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],wt[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]),
c(dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]))
n5=length(c(dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]))
#0.598

# unhappily split ::
x6=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))],dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))])
n6=length(dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))])
#0.317 #=> deviate from a consistent position in party invote, who expect to replace above loss on one deviation

y2=sum((c(n4,n5,n6)/sum(c(n4,n5,n6)))*c(x4,x5,x6))


cross_mat=rbind(c(x1,x2,x3),c(x4,x5,x6),c(x7,x8,x9))
rownames(cross_mat)=c('consistent 2','consistent 1','consistent 0')
colnames(cross_mat)=c('agree 2','agree 1','agree 0')
cross_mat=cbind(cross_mat,c(y1,y2,y3))

#effective unit sizes
cross_mat_n=rbind(
	c(
		round((n1/nrow(outvote))*length(unique(outvote$respondent))),
		round((n2/nrow(outvote))*length(unique(outvote$respondent))),
		round((n3/nrow(outvote))*length(unique(outvote$respondent)))
	),
	c(
		round((n4/nrow(outvote))*length(unique(outvote$respondent))),
		round((n5/nrow(outvote))*length(unique(outvote$respondent))),
		round((n6/nrow(outvote))*length(unique(outvote$respondent)))
	),
	c(
		round((n7/nrow(outvote))*length(unique(outvote$respondent))),
		round((n8/nrow(outvote))*length(unique(outvote$respondent))),
		round((n9/nrow(outvote))*length(unique(outvote$respondent)))
	)
)


rownames(cross_mat_n)=c('consistent 2','consistent 1','consistent 0')
colnames(cross_mat_n)=c('agree 2','agree 1','agree 0')

yy2=c(y1,y2,y3)
if(is.na(y3)){
	y3=0
}

#
weighted.mean(w=wt[which(pid3==0 & invoted=='invote' & consistency1==1 & consistency2==1 | pid3==0 & invoted=='outvote' & consistency1==1 & consistency2==1)],dv_choice[which(pid3==0 & invoted=='invote' & consistency1==1 & consistency2==1 | pid3==0 & invoted=='outvote' & consistency1==1 & consistency2==1)])
#0.493
weighted.mean(w=wt[which(pid3==0 & invoted=='invote' & (consistency1==1 & consistency2==0 | pid3==0 & consistency1==0 & consistency2==1) | invoted=='outvote' & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))],dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & consistency2==0 | pid3==0 & consistency1==0 & consistency2==1) | invoted=='outvote' & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))])
#0.507
weighted.mean(w=wt[which(pid3==0 & invoted=='invote' & consistency1==0 & consistency2==0 | pid3==0 & invoted=='outvote' & consistency1==0 & consistency2==0)],dv_choice[which(pid3==0 & invoted=='invote' & consistency1==0 & consistency2==0 | pid3==0 & invoted=='outvote' & consistency1==0 & consistency2==0)])
#0.462

# consistent positions drop off less among those who disagree w/n party
x1=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)],wt[which(pid3==0 & invoted=='outvote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)]),
c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)],
  dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)]))
n1=length(c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)],dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)]))
#0.579
x2=weighted.mean(w=
	c(wt[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))],
		wt[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))]), #consistency2==1 & agree2==1))]),
	c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))],
  	dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))])) #consistency2==1 & agree2==1))]))
n2=length(c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))],dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))]))
#0.462
x3=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)],wt[which(pid3==0 & invoted=='outvote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)]),
	c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)],
	  dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)]))
n3=length(c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)],dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)]))
#0.406

y1=sum((c(n1,n2,n3)/sum(c(n1,n2,n3)))*c(x1,x2,x3))

x7=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)],wt[which(pid3==0 & invoted=='outvote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)]),
	c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)],
	  dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)]))
n7=length(c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)],dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)]))
#0.55
x8=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))],wt[which(pid3==0 & invoted=='outvote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))]),
	c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))],
	  dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))]))
n8=length(c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))],dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))]))
#0.42
x9=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)],wt[which(pid3==0 & invoted=='outvote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)]),
	c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)],
	  dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)]))
n9=length(c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)],dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)]))
#0.371

y3=sum((c(n7,n8,n9)/sum(c(n7,n8,n9)))*c(x7,x8,x9))

# happily split :: similar split/cross-pressure
x4=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))],wt[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))]),
	c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))],
	  dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))]))
n4=length(c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))],dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))]))
#0.547 #=> deviate from a consistent position in party invote, who expect to replace above loss on one deviation

# party consistent defectors
#mean(dv_choice[which(invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))])
#0.600 #=> deviate from a consistent position in party invote, who expect is first to go on one deviation

# moderate splits
#mean(dv_choice[which(invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))])
#0.546 #=> deviate from a consistent position in party invote, who expect is first to go on one deviation

x5=weighted.mean(w=
	c(wt[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
	wt[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))],
	wt[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
	wt[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]),
				c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
					dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))],
					dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
					dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]))
n5=length(
				c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
					dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))],
					dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
					dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]))
#
#.453

# unhappily split ::
x6=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))],wt[which(pid3==0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))]),
				c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))],
					dv_choice[which(pid3==0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))]))
n6=length(c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))],dv_choice[which(pid3==0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))]))
#0.413 #=> deviate from a consistent position in party invote, who expect to replace above loss on one deviation

y2=sum((c(n4,n5,n6)/sum(c(n4,n5,n6)))*c(x4,x5,x6))


indep_mat=rbind(c(x1,x2,x3),c(x4,x5,x6),c(x7,x8,x9))
rownames(indep_mat)=c('consistent 2','consistent 1','consistent 0')
colnames(indep_mat)=c('agree 2','agree 1','agree 0')
indep_mat=cbind(indep_mat,c(y1,y2,y3))

yy3=c(y1,y2,y3)

do.ind=F
if(do.ind==T){
p1=1
p2=0
p3=0
#2 to 1; out to ind
pt=1e+09
while((yy1*p1+yy2*p2+yy3*p3)[1]>(yy1*p1+yy2*p2+yy3*p3)[3]){
	p1=p1-.01
	p2=(1-p1)*pt/(pt+1)
	p3=(1-p1)*1/(pt+1)
}

p1
#0.46
p2
#0.54
p3
#0
}

# brief CQ exploration is not really
# by CQ?
weighted.mean(w=wt[which(invoted=='invote' & cq==1 & consistency1==1 & consistency2==1)],dv_choice[which(invoted=='invote' & cq==1 & consistency1==1 & consistency2==1)])
# 0.5596222
weighted.mean(w=wt[which(invoted=='invote' & cq>1 & cq<4 & consistency1==1 & consistency2==1)],dv_choice[which(invoted=='invote' & cq>1 & cq<4 & consistency1==1 & consistency2==1)])
# 0.5506547

weighted.mean(w=wt[which(invoted=='invote' & cq==1 & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))],dv_choice[which(invoted=='invote' & cq==1 & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))])
#0.4173913
weighted.mean(w=wt[which(invoted=='invote' & cq>1 & cq<4 & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))],dv_choice[which(invoted=='invote' & cq>1 & cq<4 & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))])
#0.4291869

weighted.mean(w=wt[which(invoted=='invote' & cq==1 & consistency1==0 & consistency2==0)],dv_choice[which(invoted=='invote' & cq==1 & consistency1==0 & consistency2==0)])
# 0.2830189
weighted.mean(w=wt[which(invoted=='invote' & cq>1 & cq<4 & consistency1==0 & consistency2==0)],dv_choice[which(invoted=='invote' & cq>1 & cq<4 & consistency1==0 & consistency2==0)])
# 0.2527473


#replicate using a aggregate measure of cross-pressure,
# a) above agreement measures could have error which distorts measures of support
# b) use summary score, i.e., the average of the libcon measures to decide 'cross-pressure'
#  - set threshold at i) 25% and ii) 50% of party supporters

x1=weighted.mean(w=wt[which(invoted=='invote' & consistency==2 & crossp25==1)],dv_choice[which(invoted=='invote' & consistency==2 & crossp25==1)])
n1=length(dv_choice[which(invoted=='invote' & consistency==2 & crossp25==1)])
#0.5100464
x2=weighted.mean(w=wt[which(invoted=='invote' & consistency==2 & crossp25==0)],dv_choice[which(invoted=='invote' & consistency==2 & crossp25==0)])
n2=length(dv_choice[which(invoted=='invote' & consistency==2 & crossp25==0)])
#0.5808467

y1=sum((c(n1,n2)/sum(c(n1,n2)))*c(x1,x2))

x3=weighted.mean(w=wt[which(invoted=='invote' & consistency==1 & crossp25==1)],dv_choice[which(invoted=='invote' & consistency==1 & crossp25==1)])
n3=length(dv_choice[which(invoted=='invote' & consistency==1 & crossp25==1)])
#0.4745342
x4=weighted.mean(w=wt[which(invoted=='invote' & consistency==1 & crossp25==0)],dv_choice[which(invoted=='invote' & consistency==1 & crossp25==0)])
n4=length(dv_choice[which(invoted=='invote' & consistency==1 & crossp25==0)])
#0.4025267

y2=sum((c(n3,n4)/sum(c(n3,n4)))*c(x3,x4))

x5=weighted.mean(w=wt[which(invoted=='invote' & consistency==0 & crossp25==1)],dv_choice[which(invoted=='invote' & consistency==0 & crossp25==1)])
n5=length(dv_choice[which(invoted=='invote' & consistency==0 & crossp25==1)])
#0.4633028
x6=weighted.mean(w=wt[which(invoted=='invote' & consistency==0 & crossp25==0)],dv_choice[which(invoted=='invote' & consistency==0 & crossp25==0)])
n6=length(dv_choice[which(invoted=='invote' & consistency==0 & crossp25==0)])
# 0.2339869

y3=sum((c(n5,n6)/sum(c(n5,n6)))*c(x5,x6))


cp25_mat=rbind(c(x2,x1),c(x4,x3),c(x6,x5))
rownames(cp25_mat)=c('consistent 2','consistent 1','consistent 0')
colnames(cp25_mat)=c('not cross','cross')
cp25_mat=cbind(cp25_mat,c(y1,y2,y3))

yy1=c(y1,y2,y3)



x1=weighted.mean(w=wt[which(invoted=='invote' & consistency==2 & crossp50==1)],dv_choice[which(invoted=='invote' & consistency==2 & crossp50==1)])
n1=length(dv_choice[which(invoted=='invote' & consistency==2 & crossp50==1)])
#0.5255489
x2=weighted.mean(w=wt[which(invoted=='invote' & consistency==2 & crossp50==0)],dv_choice[which(invoted=='invote' & consistency==2 & crossp50==0)])
n2=length(dv_choice[which(invoted=='invote' & consistency==2 & crossp50==0)])
#0.6000379

y1=sum((c(n1,n2)/sum(c(n1,n2)))*c(x1,x2))

x3=weighted.mean(w=wt[which(invoted=='invote' & consistency==1 & crossp50==1)],dv_choice[which(invoted=='invote' & consistency==1 & crossp50==1)])
n3=length(dv_choice[which(invoted=='invote' & consistency==1 & crossp50==1)])
#0.4633999
x4=weighted.mean(w=wt[which(invoted=='invote' & consistency==1 & crossp50==0)],dv_choice[which(invoted=='invote' & consistency==1 & crossp50==0)])
n4=length(dv_choice[which(invoted=='invote' & consistency==1 & crossp50==0)])
#0.3777907

y2=sum((c(n3,n4)/sum(c(n3,n4)))*c(x3,x4))

x5=weighted.mean(w=wt[which(invoted=='invote' & consistency==0 & crossp50==1)],dv_choice[which(invoted=='invote' & consistency==0 & crossp50==1)])
n5=length(dv_choice[which(invoted=='invote' & consistency==0 & crossp50==1)])
#0.3604888
x6=weighted.mean(w=wt[which(invoted=='invote' & consistency==0 & crossp50==0)],dv_choice[which(invoted=='invote' & consistency==0 & crossp50==0)])
n6=length(dv_choice[which(invoted=='invote' & consistency==0 & crossp50==0)])
#0.2093496

y3=sum((c(n5,n6)/sum(c(n5,n6)))*c(x5,x6))


cp50_mat=rbind(c(x2,x1),c(x4,x3),c(x6,x5))
rownames(cp50_mat)=c('consistent 2','consistent 1','consistent 0')
colnames(cp50_mat)=c('not cross','cross')
cp50_mat=cbind(cp50_mat,c(y1,y2,y3))

yy2=c(y1,y2,y3)

print(xtable(defect_mat,digits=3),file='appendix/figures/inparty_defect_H.tex')
#> defect_mat

print(xtable(cross_mat,digits=3),file='appendix/figures/outparty_defect_H.tex')
#> cross_mat

# HERE

###########################################
#  Ideology and Electability in Primary Voting Behavior
###########################################
#  - code by S. Goggin (Aug 10, 2017)
#  - edited by J. Henderson (Aug 16, 2017)

rm(list=ls()[which(ls()!='dirs')])
library(ggplot2)
library(stringr)

# messy function to reorder by some estimate value
lableOrder=function(xmat,labels,label.groups,omits,o.column){

	# denote which label is to be omitted on the label
	for(i in 1:length(omits)){
		labels[which(labels==omits[i])]=paste('omit',labels[which(labels==omits[i])],sep='_')
	}

	# break groups into levels
	un_group=unique(label.groups)

	# this is the item to sort on, typically global or independent
	xm=xmat[,o.column]

	# vector which will contain row order
	xo=1:length(xm)

	# rearranging roworder within level
	for(j in 1:length(un_group)){
		ix=which(label.groups==un_group[j])
		if(length(ix)>2){
			ix=ix[!grepl(labels[ix],pattern='omit')]
			xo[ix]=xo[ix][order(xm[ix])]
		}
	}
	return(xmat[xo,])
}

reOrder=function(x,o){
	ix=array(NA,nrow(x))
	for(i in 1:length(o)){
		ix[i]=which(x$iv_order==o[i])
	}
	return(x[ix,])
}

setwd(dirs)

load('data/data_matrix_scored.Rdata')
scored=read.csv('data/candidate_matrix.csv')


###########################################
###First, need to stack based on candidates, not just candidate pairs (and also get text out for labels later)

#This has leaners as independents, which is incorrect
#cces_stacked$pid3clean <- ifelse(cces_stacked$pid3=="Democrat",-1,ifelse(cces_stacked$pid3=="Republican",1,0))

library(car)

###########################################
###Then, produce models w/ Clustered SEs

#Function from: http://scholar.byu.edu/jgubler/book/clustered-standard-errors-r
#Need this for clustered standard errors
clse.f <- function(dat,fm, cluster){
 require(sandwich)
 require(lmtest)
 not <- attr(fm$model,"na.action")
if( ! is.null(not)){
  cluster <- cluster[-not]
    dat <- dat[-not,]
}
 with(dat,{
 M <- length(unique(cluster))
 N <- length(cluster)
 K <- fm$rank
 dfc <- (M/(M-1))*((N-1)/(N-K))
 uj <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
 vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
 coeftest(fm, vcovCL)
 }
 )
}

data_matrix=data_matrix[,-c(grep(names(data_matrix),pattern='scores'))]
candidate_matrix=cbind(data_matrix,scored[,c('scores','scores_var','scores_policy','scores_policy_var')])

# i_policy is conjoint position in either i1 or i2
# consistency here is candidate too consistent Dem or Rep position on policy
# analogous to above variance in aggregating over candidate profiles on issues

candidate_matrix$consistency=
as.numeric(candidate_matrix$"i_lgbt"==1)*-1+
as.numeric(candidate_matrix$"i_marriage"==1)*1+
as.numeric(candidate_matrix$"i_need"==1)*-1+
as.numeric(candidate_matrix$"i_govabuse"==1)*1+
as.numeric(candidate_matrix$"i_freetrade"==1)*-1+
as.numeric(candidate_matrix$"i_unfairtrade"==1)*1+
as.numeric(candidate_matrix$"i_righttochoose"==1)*-1+
as.numeric(candidate_matrix$"i_unbornlives"==1)*1+
as.numeric(candidate_matrix$"i_raisetaxes"==1)*-1+
as.numeric(candidate_matrix$"i_cuttaxes"==1)*1+
as.numeric(candidate_matrix$"i_co2emissions"==1)*-1+
as.numeric(candidate_matrix$"i_drilling"==1)*1+
as.numeric(candidate_matrix$"i_citizenship"==1)*-1+
as.numeric(candidate_matrix$"i_bordersecurity"==1)*1+
as.numeric(candidate_matrix$"i_guncontrol"==1)*-1+
as.numeric(candidate_matrix$"i_gunrights"==1)*1+
as.numeric(candidate_matrix$"i_reducemilitary"==1)*-1+
as.numeric(candidate_matrix$"i_strengthenmilitary"==1)*1+
as.numeric(candidate_matrix$"i_policing"==1)*-1+
as.numeric(candidate_matrix$"i_criminals"==1)*1


candidate_matrix$libcon=rowMeans(cbind(candidate_matrix[,grep(names(candidate_matrix),pattern='libcon')],candidate_matrix$self_place/3))


############################################################
############################################################
###Now for Primary Elections (by PID)  -  InParty
############################################################
############################################################
###Now for Primary Elections (by PID)  -  OutParty

load('data/issueCrossPressureData.Rdata')

########################################################################
########################################################################
# matrix of two-way support by issue 1 and issue 2 by party
########################################################################
########################################################################

reps_agree=dems_agree=as.data.frame(matrix(NA,20,20))
ixa=c("need","govabuse",
"reducemilitary","strengthenmilitary",
"raisetaxes","cuttaxes",
"lgbt","marriage",
"co2emissions","drilling",
"freetrade","unfairtrade",
"righttochoose","unbornlives",
"guncontrol","gunrights",
"citizenship","bordersecurity",
"policing","criminals")

#ixb=c('libcon_need','libcon_dfns','libcon_taxs','libcon_gays','libcon_envs',
	#'libcon_trad','libcon_abrt','libcon_guns','libcon_immi','libcon_crme')


issue_mat=cbind(
		as.numeric(candidate_matrix$libcon_need<=0),
		as.numeric(candidate_matrix$libcon_need>=0),
		as.numeric(candidate_matrix$libcon_dfns<=0),
		as.numeric(candidate_matrix$libcon_dfns>=0),
		as.numeric(candidate_matrix$libcon_taxs<=0),
		as.numeric(candidate_matrix$libcon_taxs>=0),
		as.numeric(candidate_matrix$libcon_gays<=0),
		as.numeric(candidate_matrix$libcon_gays>=0),
		as.numeric(candidate_matrix$libcon_envs<=0),
		as.numeric(candidate_matrix$libcon_envs>=0),
		as.numeric(candidate_matrix$libcon_trad<=0),
		as.numeric(candidate_matrix$libcon_trad>=0),
		as.numeric(candidate_matrix$libcon_abrt<=0),
		as.numeric(candidate_matrix$libcon_abrt>=0),
		as.numeric(candidate_matrix$libcon_guns<=0),
		as.numeric(candidate_matrix$libcon_guns>=0),
		as.numeric(candidate_matrix$libcon_immi<=0),
		as.numeric(candidate_matrix$libcon_immi>=0),
		as.numeric(candidate_matrix$libcon_crme<=0),
		as.numeric(candidate_matrix$libcon_crme>=0)
)
colnames(issue_mat)=ixa
rownames(reps_agree)=rownames(dems_agree)=colnames(reps_agree)=colnames(dems_agree)=ixa


for(i in 1:20){
	for(j in i:20){
		dems_agree[i,j]=mean(issue_mat[which(candidate_matrix$pid==-1),i]==1 & issue_mat[which(candidate_matrix$pid==-1),j]==1)
		reps_agree[i,j]=mean(issue_mat[which(candidate_matrix$pid== 1),i]==1 & issue_mat[which(candidate_matrix$pid== 1),j]==1)
	}
}

############################################################
# baseline agreement measures of issue-by-issue ideology
############################################################

library(xtable)
print(xtable(dems_agree),file='appendix/figures/demsIssueAgreeMatrix.tex')
print(xtable(reps_agree),file='appendix/figures/repsIssueAgreeMatrix.tex')

load('data/sophistication_indices.Rdata')
L_ix2=indices$resp_id[indices$"L_ix2"]
M_ix2=indices$resp_id[indices$"M_ix2"]
H_ix2=indices$resp_id[indices$"H_ix2"]

############################################################
# inconsistency w/n primary and outparty comparisons
############################################################
# invote
invote$sophistication=NA
#invote$M_ix2=NA
#invote$L_ix2=NA

for(i in 1:length(H_ix2)){
	iq=which(invote$respondent==H_ix2[i])
	if(length(iq)>0){
		invote$sophistication[iq]='H'
	}
}

for(i in 1:length(M_ix2)){
	iq=which(invote$respondent==M_ix2[i])
	if(length(iq)>0){
		invote$sophistication[iq]='M'
	}
}

for(i in 1:length(L_ix2)){
	iq=which(invote$respondent==L_ix2[i])
	if(length(iq)>0){
		invote$sophistication[iq]='L'
	}
}



# outvote
outvote$sophistication=NA
#outvote$M_ix2=NA
#outvote$L_ix2=NA

for(i in 1:length(H_ix2)){
	iq=which(outvote$respondent==H_ix2[i])
	if(length(iq)>0){
		outvote$sophistication[iq]='H'
	}
}

for(i in 1:length(M_ix2)){
	iq=which(outvote$respondent==M_ix2[i])
	if(length(iq)>0){
		outvote$sophistication[iq]='M'
	}
}

for(i in 1:length(L_ix2)){
	iq=which(outvote$respondent==L_ix2[i])
	if(length(iq)>0){
		outvote$sophistication[iq]='L'
	}
}

# H sophistication

reps <- subset(invote, invote$pid3==1 & invote$sophistication=='M')
dems <- subset(invote, invote$pid3==-1 & invote$sophistication=='M')
inds <- subset(invote, invote$pid3==0)

dems$consistency1=as.numeric(dems$consistency1==-1)
dems$consistency2=as.numeric(dems$consistency2==-1)
reps$consistency1=as.numeric(reps$consistency1== 1)
reps$consistency2=as.numeric(reps$consistency2== 1)

inds$consistency1[which(inds$pty==1)]=as.numeric(inds$consistency1[which(inds$pty==1)]== 1)
inds$consistency2[which(inds$pty==1)]=as.numeric(inds$consistency2[which(inds$pty==1)]== 1)
inds$consistency1[which(inds$pty==0)]=as.numeric(inds$consistency1[which(inds$pty==0)]==-1)
inds$consistency2[which(inds$pty==0)]=as.numeric(inds$consistency2[which(inds$pty==0)]==-1)


reps$consistency=sign(reps$consistency)+1
dems$consistency=abs(sign(dems$consistency)-1)

inds$consistency[which(inds$pty==1)]=sign(inds$consistency[which(inds$pty==1)])+1
inds$consistency[which(inds$pty==0)]=abs(sign(inds$consistency[which(inds$pty==0)])-1)

invote=rbind(dems,reps,inds)

############################################################
# inconsistency w/n primary and outparty comparisons
############################################################
# outvote

dems <- subset(outvote, outvote$pid3==1 & outvote$sophistication=='M')
reps <- subset(outvote, outvote$pid3==-1 & outvote$sophistication=='M')
inds <- subset(outvote, outvote$pid3==0)

dems$consistency1=as.numeric(dems$consistency1==-1)
dems$consistency2=as.numeric(dems$consistency2==-1)
reps$consistency1=as.numeric(reps$consistency1== 1)
reps$consistency2=as.numeric(reps$consistency2== 1)

inds$consistency1[which(inds$pty==1)]=as.numeric(inds$consistency1[which(inds$pty==1)]== 1)
inds$consistency2[which(inds$pty==1)]=as.numeric(inds$consistency2[which(inds$pty==1)]== 1)
inds$consistency1[which(inds$pty==0)]=as.numeric(inds$consistency1[which(inds$pty==0)]==-1)
inds$consistency2[which(inds$pty==0)]=as.numeric(inds$consistency2[which(inds$pty==0)]==-1)

reps$consistency=sign(reps$consistency)+1
dems$consistency=abs(sign(dems$consistency)-1)

inds$consistency[which(inds$pty==1)]=sign(inds$consistency[which(inds$pty==1)])+1
inds$consistency[which(inds$pty==0)]=abs(sign(inds$consistency[which(inds$pty==0)])-1)

outvote=rbind(dems,reps,inds)

invote=cbind(invote,'invote')
outvote=cbind(outvote,'outvote')
names(outvote)[ncol(outvote)]=names(invote)[ncol(invote)]='invoted'
invote[,ncol(invote)]=as.character(invote[,ncol(invote)])
outvote[,ncol(outvote)]=as.character(outvote[,ncol(outvote)])

vote=rbind(invote,outvote)

################################################################
# consistency pooling dems and reps
################################################################

ds=quantile(vote$libcon[which(vote$pid==-1)],pr=c(.75,.5))
rs=quantile(vote$libcon[which(vote$pid== 1)],pr=c(.25,.5))

vote$crossp25=NA
vote$crossp50=NA

vote$crossp25[which(vote$pid==-1)]=as.numeric(vote$libcon[which(vote$pid==-1)]>ds[1])
vote$crossp50[which(vote$pid==-1)]=as.numeric(vote$libcon[which(vote$pid==-1)]>ds[2])

vote$crossp25[which(vote$pid== 1)]=as.numeric(vote$libcon[which(vote$pid== 1)]<rs[1])
vote$crossp50[which(vote$pid== 1)]=as.numeric(vote$libcon[which(vote$pid== 1)]<rs[2])

###TBD predprob
#
vote$c0a0=vote$consistency1+vote$consistency2==0 & vote$agree1+vote$agree2==0
vote$c0a1=vote$consistency1+vote$consistency2==0 & vote$agree1+vote$agree2==1
vote$c0a2=vote$consistency1+vote$consistency2==0 & vote$agree1+vote$agree2==2

vote$c1a0=vote$consistency1+vote$consistency2==1 & vote$agree1+vote$agree2==0
vote$c1a2=vote$consistency1+vote$consistency2==1 & vote$agree1+vote$agree2==2

vote$c1a1=vote$consistency1+vote$consistency2==1 & vote$agree1+vote$agree2==1
## does agreement x constituency matter on the
# 1 0 | 0 1
# 0 1 | 1 0

# 1 0 | 1 0
# 0 1 | 0 1
vote$c1a1_align=(vote$consistency1==1 & vote$agree1==1 & vote$consistency2==0 & vote$agree2==0) | (vote$consistency1==0 & vote$agree1==0 & vote$consistency2==1 & vote$agree2==1)
vote$c1a1_misal=(vote$consistency1==0 & vote$agree1==1 & vote$consistency2==1 & vote$agree2==0) | (vote$consistency1==1 & vote$agree1==0 & vote$consistency2==0 & vote$agree2==1)

vote$c2a0=vote$consistency1+vote$consistency2==2 & vote$agree1+vote$agree2==0
vote$c2a1=vote$consistency1+vote$consistency2==2 & vote$agree1+vote$agree2==1
vote$c2a2=vote$consistency1+vote$consistency2==2 & vote$agree1+vote$agree2==2


attach(vote)
pred_prob_invote <- lm(dv_choice~
	#vote$c0a0+
	vote$c0a1+vote$c0a2+
	vote$c1a0+vote$c1a1+vote$c1a2+
	vote$c2a0+vote$c2a1+vote$c2a2,subset=which(vote$invoted=='invote'))
pred_prob_invote_clse <- clse.f(vote[which(vote$invoted=='invote'),],pred_prob_invote,respondent[which(vote$invoted=='invote')])


###
attach(vote)
# fixing 2 consistent positions :: co-party and out-party voters

##########################################
# in party
##########################################

weighted.mean(w=wt[which(pid3!=0 & invoted=='invote' & consistency1==1 & consistency2==1)],dv_choice[which(pid3!=0 & invoted=='invote' & consistency1==1 & consistency2==1)])
#0.570
weighted.mean(w=wt[which(pid3!=0 & invoted=='invote' & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))],dv_choice[which(pid3!=0 & invoted=='invote' & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))])
#0.443
weighted.mean(w=wt[which(pid3!=0 & invoted=='invote' & consistency1==0 & consistency2==0)],dv_choice[which(pid3!=0 & invoted=='invote' & consistency1==0 & consistency2==0)])
#0.304

# consistent positions drop off less among those who disagree w/n party
x1=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)],dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)])
n1=length(dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)])
#0.597
x2=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))],dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))])
n2=length(dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))])
#0.520
x3=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)],dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)])
n3=length(dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)])
#0.444

y1=sum((c(n1,n2,n3)/sum(c(n1,n2,n3)))*c(x1,x2,x3))


x7=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)],dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)])
n7=length(dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)])
#0.347
x8=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))],dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))])
n8=length(dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))])
#0.327
x9=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)],dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)])
n9=length(dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)])
#0.203

y3=sum((c(n7,n8,n9)/sum(c(n7,n8,n9)))*c(x7,x8,x9))


# happily split :: similar split/cross-pressure
x4=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))],dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))])
n4=length(dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))])
#0.461 #=> deviate from a consistent position in party primary, who expect to replace above loss on one deviation

# merge two-way split
# party consistent defectors
#mean(dv_choice[which(invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))])
#0.401 #=> deviate from a consistent position in party primary, who expect is first to go on one deviation

# moderate splits
#mean(dv_choice[which(invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))])
#0.434 #=> deviate from a consistent position in party primary, who expect is first to go on one deviation

x5=weighted.mean(w=c(wt[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],wt[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]),
c(dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]))
n5=length(c(dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]))

# unhappily split ::
x6=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))],dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))])
n6=length(dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))])
#0.369 #=> deviate from a consistent position in party invote, who expect to replace above loss on one deviation

y2=sum((c(n4,n5,n6)/sum(c(n4,n5,n6)))*c(x4,x5,x6))


defect_mat=rbind(c(x1,x2,x3),c(x4,x5,x6),c(x7,x8,x9))
rownames(defect_mat)=c('consistent 2','consistent 1','consistent 0')
colnames(defect_mat)=c('agree 2','agree 1','agree 0')
defect_mat=cbind(defect_mat,c(y1,y2,y3))

#effective unit sizes
defect_mat_n=rbind(
	c(
		round((n1/nrow(invote))*length(unique(invote$respondent))),
		round((n2/nrow(invote))*length(unique(invote$respondent))),
		round((n3/nrow(invote))*length(unique(invote$respondent)))
	),
	c(
		round((n4/nrow(invote))*length(unique(invote$respondent))),
		round((n5/nrow(invote))*length(unique(invote$respondent))),
		round((n6/nrow(invote))*length(unique(invote$respondent)))
	),
	c(
		round((n7/nrow(invote))*length(unique(invote$respondent))),
		round((n8/nrow(invote))*length(unique(invote$respondent))),
		round((n9/nrow(invote))*length(unique(invote$respondent)))
	)
)


rownames(defect_mat_n)=c('consistent 2','consistent 1','consistent 0')
colnames(defect_mat_n)=c('agree 2','agree 1','agree 0')
#defect_mat_n=cbind(defect_mat_n,c(y1,y2,y3))
## exploring the outparty conditions

yy1=c(y1,y2,y3)


##########################################
# out party
##########################################
# above is just co-partisans
# below merges out-partisans

#
weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & consistency1==1 & consistency2==1)],dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==1 & consistency2==1)])
#0.415
weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))],dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))])
#0.544
weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & consistency1==0 & consistency2==0)],dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==0 & consistency2==0)])
#0.654

# consistent positions drop off less among those who disagree w/n party
x1=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)],dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)])
n1=length(dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)])
#0.523
x2=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))],dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))])
n2=length(dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))])
#0.449
x3=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)],dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)])
n3=length(dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)])
#0.347

y1=sum((c(n1,n2,n3)/sum(c(n1,n2,n3)))*c(x1,x2,x3))

x7=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)],dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)])
n7=length(dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)])
#0.712
x8=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))],dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))])
n8=length(dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))])
#0.586
x9=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)],dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)])
n9=length(dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)])
#0.535

y3=sum((c(n7,n8,n9)/sum(c(n7,n8,n9)))*c(x7,x8,x9))


# happily split :: similar split/cross-pressure
x4=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))],dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))])
n4=length(dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))])
#0.543 #=> deviate from a consistent position in party invote, who expect to replace above loss on one deviation

# party consistent defectors
#mean(dv_choice[which(invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))])
#0.600 #=> deviate from a consistent position in party invote, who expect is first to go on one deviation

# moderate splits
#mean(dv_choice[which(invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))])
#0.546 #=> deviate from a consistent position in party invote, who expect is first to go on one deviation

x5=weighted.mean(w=c(wt[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],wt[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]),
c(dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]))
n5=length(c(dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]))

# unhappily split ::
x6=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))],dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))])
n6=length(dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))])
#0.498 #=> deviate from a consistent position in party invote, who expect to replace above loss on one deviation

y2=sum((c(n4,n5,n6)/sum(c(n4,n5,n6)))*c(x4,x5,x6))


cross_mat=rbind(c(x1,x2,x3),c(x4,x5,x6),c(x7,x8,x9))
rownames(cross_mat)=c('consistent 2','consistent 1','consistent 0')
colnames(cross_mat)=c('agree 2','agree 1','agree 0')
cross_mat=cbind(cross_mat,c(y1,y2,y3))

#effective unit sizes
cross_mat_n=rbind(
	c(
		round((n1/nrow(outvote))*length(unique(outvote$respondent))),
		round((n2/nrow(outvote))*length(unique(outvote$respondent))),
		round((n3/nrow(outvote))*length(unique(outvote$respondent)))
	),
	c(
		round((n4/nrow(outvote))*length(unique(outvote$respondent))),
		round((n5/nrow(outvote))*length(unique(outvote$respondent))),
		round((n6/nrow(outvote))*length(unique(outvote$respondent)))
	),
	c(
		round((n7/nrow(outvote))*length(unique(outvote$respondent))),
		round((n8/nrow(outvote))*length(unique(outvote$respondent))),
		round((n9/nrow(outvote))*length(unique(outvote$respondent)))
	)
)


rownames(cross_mat_n)=c('consistent 2','consistent 1','consistent 0')
colnames(cross_mat_n)=c('agree 2','agree 1','agree 0')

yy2=c(y1,y2,y3)


#
weighted.mean(w=wt[which(pid3==0 & invoted=='invote' & consistency1==1 & consistency2==1 | pid3==0 & invoted=='outvote' & consistency1==1 & consistency2==1)],dv_choice[which(pid3==0 & invoted=='invote' & consistency1==1 & consistency2==1 | pid3==0 & invoted=='outvote' & consistency1==1 & consistency2==1)])
#0.415
weighted.mean(w=wt[which(pid3==0 & invoted=='invote' & (consistency1==1 & consistency2==0 | pid3==0 & consistency1==0 & consistency2==1) | invoted=='outvote' & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))],dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & consistency2==0 | pid3==0 & consistency1==0 & consistency2==1) | invoted=='outvote' & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))])
#0.544
weighted.mean(w=wt[which(pid3==0 & invoted=='invote' & consistency1==0 & consistency2==0 | pid3==0 & invoted=='outvote' & consistency1==0 & consistency2==0)],dv_choice[which(pid3==0 & invoted=='invote' & consistency1==0 & consistency2==0 | pid3==0 & invoted=='outvote' & consistency1==0 & consistency2==0)])
#0.654

# consistent positions drop off less among those who disagree w/n party
x1=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)],wt[which(pid3==0 & invoted=='outvote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)]),
c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)],
  dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)]))
n1=length(c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)],dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)]))
#0.523
x2=weighted.mean(w=
	c(wt[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))],
		wt[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))]), #consistency2==1 & agree2==1))]),
	c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))],
  	dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))])) #consistency2==1 & agree2==1))]))
n2=length(c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))],dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))]))
#0.449
x3=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)],wt[which(pid3==0 & invoted=='outvote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)]),
	c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)],
	  dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)]))
n3=length(c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)],dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)]))
#0.347

y1=sum((c(n1,n2,n3)/sum(c(n1,n2,n3)))*c(x1,x2,x3))

x7=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)],wt[which(pid3==0 & invoted=='outvote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)]),
	c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)],
	  dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)]))
n7=length(c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)],dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)]))
#0.712
x8=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))],wt[which(pid3==0 & invoted=='outvote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))]),
	c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))],
	  dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))]))
n8=length(c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))],dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))]))
#0.586
x9=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)],wt[which(pid3==0 & invoted=='outvote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)]),
	c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)],
	  dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)]))
n9=length(c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)],dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)]))
#0.535

y3=sum((c(n7,n8,n9)/sum(c(n7,n8,n9)))*c(x7,x8,x9))

# happily split :: similar split/cross-pressure
x4=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))],wt[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))]),
	c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))],
	  dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))]))
n4=length(c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))],dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))]))
#0.543 #=> deviate from a consistent position in party invote, who expect to replace above loss on one deviation

# party consistent defectors
#mean(dv_choice[which(invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))])
#0.600 #=> deviate from a consistent position in party invote, who expect is first to go on one deviation

# moderate splits
#mean(dv_choice[which(invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))])
#0.546 #=> deviate from a consistent position in party invote, who expect is first to go on one deviation

x5=weighted.mean(w=
	c(wt[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
	wt[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))],
	wt[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
	wt[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]),
				c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
					dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))],
					dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
					dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]))
n5=length(
				c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
					dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))],
					dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
					dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]))
#

# unhappily split ::
x6=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))],wt[which(pid3==0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))]),
				c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))],
					dv_choice[which(pid3==0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))]))
n6=length(c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))],dv_choice[which(pid3==0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))]))
#0.498 #=> deviate from a consistent position in party invote, who expect to replace above loss on one deviation

y2=sum((c(n4,n5,n6)/sum(c(n4,n5,n6)))*c(x4,x5,x6))


indep_mat=rbind(c(x1,x2,x3),c(x4,x5,x6),c(x7,x8,x9))
rownames(indep_mat)=c('consistent 2','consistent 1','consistent 0')
colnames(indep_mat)=c('agree 2','agree 1','agree 0')
indep_mat=cbind(indep_mat,c(y1,y2,y3))

yy3=c(y1,y2,y3)

p1=1
p2=0
p3=0
#2 to 1; out to ind
pt=1e+09
while((yy1*p1+yy2*p2+yy3*p3)[1]>(yy1*p1+yy2*p2+yy3*p3)[3]){
	p1=p1-.01
	p2=(1-p1)*pt/(pt+1)
	p3=(1-p1)*1/(pt+1)
}

p1
#0.46
p2
#0.54
p3
#0

# brief CQ exploration is not really
# by CQ?
weighted.mean(w=wt[which(invoted=='invote' & cq==1 & consistency1==1 & consistency2==1)],dv_choice[which(invoted=='invote' & cq==1 & consistency1==1 & consistency2==1)])
weighted.mean(w=wt[which(invoted=='invote' & cq>1 & cq<4 & consistency1==1 & consistency2==1)],dv_choice[which(invoted=='invote' & cq>1 & cq<4 & consistency1==1 & consistency2==1)])
#0.563

weighted.mean(w=wt[which(invoted=='invote' & cq==1 & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))],dv_choice[which(invoted=='invote' & cq==1 & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))])
weighted.mean(w=wt[which(invoted=='invote' & cq>1 & cq<4 & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))],dv_choice[which(invoted=='invote' & cq>1 & cq<4 & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))])

#0.420
weighted.mean(w=wt[which(invoted=='invote' & cq==1 & consistency1==0 & consistency2==0)],dv_choice[which(invoted=='invote' & cq==1 & consistency1==0 & consistency2==0)])
weighted.mean(w=wt[which(invoted=='invote' & cq>1 & cq<4 & consistency1==0 & consistency2==0)],dv_choice[which(invoted=='invote' & cq>1 & cq<4 & consistency1==0 & consistency2==0)])
#0.285


#replicate using a aggregate measure of cross-pressure,
# a) above agreement measures could have error which distorts measures of support
# b) use summary score, i.e., the average of the libcon measures to decide 'cross-pressure'
#  - set threshold at i) 25% and ii) 50% of party supporters

x1=weighted.mean(w=wt[which(invoted=='invote' & consistency==2 & crossp25==1)],dv_choice[which(invoted=='invote' & consistency==2 & crossp25==1)])
n1=length(dv_choice[which(invoted=='invote' & consistency==2 & crossp25==1)])
#0.5100464
x2=weighted.mean(w=wt[which(invoted=='invote' & consistency==2 & crossp25==0)],dv_choice[which(invoted=='invote' & consistency==2 & crossp25==0)])
n2=length(dv_choice[which(invoted=='invote' & consistency==2 & crossp25==0)])
#0.5808467

y1=sum((c(n1,n2)/sum(c(n1,n2)))*c(x1,x2))

x3=weighted.mean(w=wt[which(invoted=='invote' & consistency==1 & crossp25==1)],dv_choice[which(invoted=='invote' & consistency==1 & crossp25==1)])
n3=length(dv_choice[which(invoted=='invote' & consistency==1 & crossp25==1)])
#0.4745342
x4=weighted.mean(w=wt[which(invoted=='invote' & consistency==1 & crossp25==0)],dv_choice[which(invoted=='invote' & consistency==1 & crossp25==0)])
n4=length(dv_choice[which(invoted=='invote' & consistency==1 & crossp25==0)])
#0.4025267

y2=sum((c(n3,n4)/sum(c(n3,n4)))*c(x3,x4))

x5=weighted.mean(w=wt[which(invoted=='invote' & consistency==0 & crossp25==1)],dv_choice[which(invoted=='invote' & consistency==0 & crossp25==1)])
n5=length(dv_choice[which(invoted=='invote' & consistency==0 & crossp25==1)])
#0.4633028
x6=weighted.mean(w=wt[which(invoted=='invote' & consistency==0 & crossp25==0)],dv_choice[which(invoted=='invote' & consistency==0 & crossp25==0)])
n6=length(dv_choice[which(invoted=='invote' & consistency==0 & crossp25==0)])
# 0.2339869

y3=sum((c(n5,n6)/sum(c(n5,n6)))*c(x5,x6))


cp25_mat=rbind(c(x2,x1),c(x4,x3),c(x6,x5))
rownames(cp25_mat)=c('consistent 2','consistent 1','consistent 0')
colnames(cp25_mat)=c('not cross','cross')
cp25_mat=cbind(cp25_mat,c(y1,y2,y3))

yy1=c(y1,y2,y3)



x1=weighted.mean(w=wt[which(invoted=='invote' & consistency==2 & crossp50==1)],dv_choice[which(invoted=='invote' & consistency==2 & crossp50==1)])
n1=length(dv_choice[which(invoted=='invote' & consistency==2 & crossp50==1)])
#0.5255489
x2=weighted.mean(w=wt[which(invoted=='invote' & consistency==2 & crossp50==0)],dv_choice[which(invoted=='invote' & consistency==2 & crossp50==0)])
n2=length(dv_choice[which(invoted=='invote' & consistency==2 & crossp50==0)])
#0.6000379

y1=sum((c(n1,n2)/sum(c(n1,n2)))*c(x1,x2))

x3=weighted.mean(w=wt[which(invoted=='invote' & consistency==1 & crossp50==1)],dv_choice[which(invoted=='invote' & consistency==1 & crossp50==1)])
n3=length(dv_choice[which(invoted=='invote' & consistency==1 & crossp50==1)])
#0.4633999
x4=weighted.mean(w=wt[which(invoted=='invote' & consistency==1 & crossp50==0)],dv_choice[which(invoted=='invote' & consistency==1 & crossp50==0)])
n4=length(dv_choice[which(invoted=='invote' & consistency==1 & crossp50==0)])
#0.3777907

y2=sum((c(n3,n4)/sum(c(n3,n4)))*c(x3,x4))

x5=weighted.mean(w=wt[which(invoted=='invote' & consistency==0 & crossp50==1)],dv_choice[which(invoted=='invote' & consistency==0 & crossp50==1)])
n5=length(dv_choice[which(invoted=='invote' & consistency==0 & crossp50==1)])
#0.3604888
x6=weighted.mean(w=wt[which(invoted=='invote' & consistency==0 & crossp50==0)],dv_choice[which(invoted=='invote' & consistency==0 & crossp50==0)])
n6=length(dv_choice[which(invoted=='invote' & consistency==0 & crossp50==0)])
#0.2093496

y3=sum((c(n5,n6)/sum(c(n5,n6)))*c(x5,x6))


cp50_mat=rbind(c(x2,x1),c(x4,x3),c(x6,x5))
rownames(cp50_mat)=c('consistent 2','consistent 1','consistent 0')
colnames(cp50_mat)=c('not cross','cross')
cp50_mat=cbind(cp50_mat,c(y1,y2,y3))

yy2=c(y1,y2,y3)

print(xtable(defect_mat,digits=3),file='appendix/figures/inparty_defect_M.tex')
#> defect_mat

print(xtable(cross_mat,digits=3),file='appendix/figures/outparty_defect_M.tex')
#> cross_mat

# HERE

###########################################
#  Ideology and Electability in Primary Voting Behavior
###########################################
#  - code by S. Goggin (Aug 10, 2017)
#  - edited by J. Henderson (Aug 16, 2017)

rm(list=ls()[which(ls()!='dirs')])
library(ggplot2)
library(stringr)

# messy function to reorder by some estimate value
lableOrder=function(xmat,labels,label.groups,omits,o.column){

	# denote which label is to be omitted on the label
	for(i in 1:length(omits)){
		labels[which(labels==omits[i])]=paste('omit',labels[which(labels==omits[i])],sep='_')
	}

	# break groups into levels
	un_group=unique(label.groups)

	# this is the item to sort on, typically global or independent
	xm=xmat[,o.column]

	# vector which will contain row order
	xo=1:length(xm)

	# rearranging roworder within level
	for(j in 1:length(un_group)){
		ix=which(label.groups==un_group[j])
		if(length(ix)>2){
			ix=ix[!grepl(labels[ix],pattern='omit')]
			xo[ix]=xo[ix][order(xm[ix])]
		}
	}
	return(xmat[xo,])
}

reOrder=function(x,o){
	ix=array(NA,nrow(x))
	for(i in 1:length(o)){
		ix[i]=which(x$iv_order==o[i])
	}
	return(x[ix,])
}

setwd(dirs)

load('data/data_matrix_scored.Rdata')
scored=read.csv('data/candidate_matrix.csv')


###########################################
###First, need to stack based on candidates, not just candidate pairs (and also get text out for labels later)

#This has leaners as independents, which is incorrect
#cces_stacked$pid3clean <- ifelse(cces_stacked$pid3=="Democrat",-1,ifelse(cces_stacked$pid3=="Republican",1,0))

library(car)

###########################################
###Then, produce models w/ Clustered SEs

#Function from: http://scholar.byu.edu/jgubler/book/clustered-standard-errors-r
#Need this for clustered standard errors
clse.f <- function(dat,fm, cluster){
 require(sandwich)
 require(lmtest)
 not <- attr(fm$model,"na.action")
if( ! is.null(not)){
  cluster <- cluster[-not]
    dat <- dat[-not,]
}
 with(dat,{
 M <- length(unique(cluster))
 N <- length(cluster)
 K <- fm$rank
 dfc <- (M/(M-1))*((N-1)/(N-K))
 uj <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
 vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
 coeftest(fm, vcovCL)
 }
 )
}

data_matrix=data_matrix[,-c(grep(names(data_matrix),pattern='scores'))]
candidate_matrix=cbind(data_matrix,scored[,c('scores','scores_var','scores_policy','scores_policy_var')])

# i_policy is conjoint position in either i1 or i2
# consistency here is candidate too consistent Dem or Rep position on policy
# analogous to above variance in aggregating over candidate profiles on issues

candidate_matrix$consistency=
as.numeric(candidate_matrix$"i_lgbt"==1)*-1+
as.numeric(candidate_matrix$"i_marriage"==1)*1+
as.numeric(candidate_matrix$"i_need"==1)*-1+
as.numeric(candidate_matrix$"i_govabuse"==1)*1+
as.numeric(candidate_matrix$"i_freetrade"==1)*-1+
as.numeric(candidate_matrix$"i_unfairtrade"==1)*1+
as.numeric(candidate_matrix$"i_righttochoose"==1)*-1+
as.numeric(candidate_matrix$"i_unbornlives"==1)*1+
as.numeric(candidate_matrix$"i_raisetaxes"==1)*-1+
as.numeric(candidate_matrix$"i_cuttaxes"==1)*1+
as.numeric(candidate_matrix$"i_co2emissions"==1)*-1+
as.numeric(candidate_matrix$"i_drilling"==1)*1+
as.numeric(candidate_matrix$"i_citizenship"==1)*-1+
as.numeric(candidate_matrix$"i_bordersecurity"==1)*1+
as.numeric(candidate_matrix$"i_guncontrol"==1)*-1+
as.numeric(candidate_matrix$"i_gunrights"==1)*1+
as.numeric(candidate_matrix$"i_reducemilitary"==1)*-1+
as.numeric(candidate_matrix$"i_strengthenmilitary"==1)*1+
as.numeric(candidate_matrix$"i_policing"==1)*-1+
as.numeric(candidate_matrix$"i_criminals"==1)*1


candidate_matrix$libcon=rowMeans(cbind(candidate_matrix[,grep(names(candidate_matrix),pattern='libcon')],candidate_matrix$self_place/3))


############################################################
############################################################
###Now for Primary Elections (by PID)  -  InParty
############################################################
############################################################
###Now for Primary Elections (by PID)  -  OutParty

load('data/issueCrossPressureData.Rdata')

########################################################################
########################################################################
# matrix of two-way support by issue 1 and issue 2 by party
########################################################################
########################################################################

reps_agree=dems_agree=as.data.frame(matrix(NA,20,20))
ixa=c("need","govabuse",
"reducemilitary","strengthenmilitary",
"raisetaxes","cuttaxes",
"lgbt","marriage",
"co2emissions","drilling",
"freetrade","unfairtrade",
"righttochoose","unbornlives",
"guncontrol","gunrights",
"citizenship","bordersecurity",
"policing","criminals")

#ixb=c('libcon_need','libcon_dfns','libcon_taxs','libcon_gays','libcon_envs',
	#'libcon_trad','libcon_abrt','libcon_guns','libcon_immi','libcon_crme')


issue_mat=cbind(
		as.numeric(candidate_matrix$libcon_need<=0),
		as.numeric(candidate_matrix$libcon_need>=0),
		as.numeric(candidate_matrix$libcon_dfns<=0),
		as.numeric(candidate_matrix$libcon_dfns>=0),
		as.numeric(candidate_matrix$libcon_taxs<=0),
		as.numeric(candidate_matrix$libcon_taxs>=0),
		as.numeric(candidate_matrix$libcon_gays<=0),
		as.numeric(candidate_matrix$libcon_gays>=0),
		as.numeric(candidate_matrix$libcon_envs<=0),
		as.numeric(candidate_matrix$libcon_envs>=0),
		as.numeric(candidate_matrix$libcon_trad<=0),
		as.numeric(candidate_matrix$libcon_trad>=0),
		as.numeric(candidate_matrix$libcon_abrt<=0),
		as.numeric(candidate_matrix$libcon_abrt>=0),
		as.numeric(candidate_matrix$libcon_guns<=0),
		as.numeric(candidate_matrix$libcon_guns>=0),
		as.numeric(candidate_matrix$libcon_immi<=0),
		as.numeric(candidate_matrix$libcon_immi>=0),
		as.numeric(candidate_matrix$libcon_crme<=0),
		as.numeric(candidate_matrix$libcon_crme>=0)
)
colnames(issue_mat)=ixa
rownames(reps_agree)=rownames(dems_agree)=colnames(reps_agree)=colnames(dems_agree)=ixa


for(i in 1:20){
	for(j in i:20){
		dems_agree[i,j]=mean(issue_mat[which(candidate_matrix$pid==-1),i]==1 & issue_mat[which(candidate_matrix$pid==-1),j]==1)
		reps_agree[i,j]=mean(issue_mat[which(candidate_matrix$pid== 1),i]==1 & issue_mat[which(candidate_matrix$pid== 1),j]==1)
	}
}

############################################################
# baseline agreement measures of issue-by-issue ideology
############################################################

library(xtable)
print(xtable(dems_agree),file='appendix/figures/demsIssueAgreeMatrix.tex')
print(xtable(reps_agree),file='appendix/figures/repsIssueAgreeMatrix.tex')

load('data/sophistication_indices.Rdata')
L_ix2=indices$resp_id[indices$"L_ix2"]
M_ix2=indices$resp_id[indices$"M_ix2"]
H_ix2=indices$resp_id[indices$"H_ix2"]

############################################################
# inconsistency w/n primary and outparty comparisons
############################################################
# invote
invote$sophistication=NA
#invote$M_ix2=NA
#invote$L_ix2=NA

for(i in 1:length(H_ix2)){
	iq=which(invote$respondent==H_ix2[i])
	if(length(iq)>0){
		invote$sophistication[iq]='H'
	}
}

for(i in 1:length(M_ix2)){
	iq=which(invote$respondent==M_ix2[i])
	if(length(iq)>0){
		invote$sophistication[iq]='M'
	}
}

for(i in 1:length(L_ix2)){
	iq=which(invote$respondent==L_ix2[i])
	if(length(iq)>0){
		invote$sophistication[iq]='L'
	}
}


# outvote
outvote$sophistication=NA
#outvote$M_ix2=NA
#outvote$L_ix2=NA

for(i in 1:length(H_ix2)){
	iq=which(outvote$respondent==H_ix2[i])
	if(length(iq)>0){
		outvote$sophistication[iq]='H'
	}
}

for(i in 1:length(M_ix2)){
	iq=which(outvote$respondent==M_ix2[i])
	if(length(iq)>0){
		outvote$sophistication[iq]='M'
	}
}

for(i in 1:length(L_ix2)){
	iq=which(outvote$respondent==L_ix2[i])
	if(length(iq)>0){
		outvote$sophistication[iq]='L'
	}
}

# H sophistication

reps <- subset(invote, invote$pid3==1 & invote$sophistication=='L')
dems <- subset(invote, invote$pid3==-1 & invote$sophistication=='L')
inds <- subset(invote, invote$pid3==0)

dems$consistency1=as.numeric(dems$consistency1==-1)
dems$consistency2=as.numeric(dems$consistency2==-1)
reps$consistency1=as.numeric(reps$consistency1== 1)
reps$consistency2=as.numeric(reps$consistency2== 1)

inds$consistency1[which(inds$pty==1)]=as.numeric(inds$consistency1[which(inds$pty==1)]== 1)
inds$consistency2[which(inds$pty==1)]=as.numeric(inds$consistency2[which(inds$pty==1)]== 1)
inds$consistency1[which(inds$pty==0)]=as.numeric(inds$consistency1[which(inds$pty==0)]==-1)
inds$consistency2[which(inds$pty==0)]=as.numeric(inds$consistency2[which(inds$pty==0)]==-1)


reps$consistency=sign(reps$consistency)+1
dems$consistency=abs(sign(dems$consistency)-1)

inds$consistency[which(inds$pty==1)]=sign(inds$consistency[which(inds$pty==1)])+1
inds$consistency[which(inds$pty==0)]=abs(sign(inds$consistency[which(inds$pty==0)])-1)

invote=rbind(dems,reps,inds)

############################################################
# inconsistency w/n primary and outparty comparisons
############################################################
# outvote

dems <- subset(outvote, outvote$pid3==1 & outvote$sophistication=='L')
reps <- subset(outvote, outvote$pid3==-1 & outvote$sophistication=='L')
inds <- subset(outvote, outvote$pid3==0)

dems$consistency1=as.numeric(dems$consistency1==-1)
dems$consistency2=as.numeric(dems$consistency2==-1)
reps$consistency1=as.numeric(reps$consistency1== 1)
reps$consistency2=as.numeric(reps$consistency2== 1)

inds$consistency1[which(inds$pty==1)]=as.numeric(inds$consistency1[which(inds$pty==1)]== 1)
inds$consistency2[which(inds$pty==1)]=as.numeric(inds$consistency2[which(inds$pty==1)]== 1)
inds$consistency1[which(inds$pty==0)]=as.numeric(inds$consistency1[which(inds$pty==0)]==-1)
inds$consistency2[which(inds$pty==0)]=as.numeric(inds$consistency2[which(inds$pty==0)]==-1)

reps$consistency=sign(reps$consistency)+1
dems$consistency=abs(sign(dems$consistency)-1)

inds$consistency[which(inds$pty==1)]=sign(inds$consistency[which(inds$pty==1)])+1
inds$consistency[which(inds$pty==0)]=abs(sign(inds$consistency[which(inds$pty==0)])-1)

outvote=rbind(dems,reps,inds)

invote=cbind(invote,'invote')
outvote=cbind(outvote,'outvote')
names(outvote)[ncol(outvote)]=names(invote)[ncol(invote)]='invoted'
invote[,ncol(invote)]=as.character(invote[,ncol(invote)])
outvote[,ncol(outvote)]=as.character(outvote[,ncol(outvote)])

vote=rbind(invote,outvote)

################################################################
# consistency pooling dems and reps
################################################################

ds=quantile(vote$libcon[which(vote$pid==-1)],pr=c(.75,.5))
rs=quantile(vote$libcon[which(vote$pid== 1)],pr=c(.25,.5))

vote$crossp25=NA
vote$crossp50=NA

vote$crossp25[which(vote$pid==-1)]=as.numeric(vote$libcon[which(vote$pid==-1)]>ds[1])
vote$crossp50[which(vote$pid==-1)]=as.numeric(vote$libcon[which(vote$pid==-1)]>ds[2])

vote$crossp25[which(vote$pid== 1)]=as.numeric(vote$libcon[which(vote$pid== 1)]<rs[1])
vote$crossp50[which(vote$pid== 1)]=as.numeric(vote$libcon[which(vote$pid== 1)]<rs[2])

###TBD predprob
#
vote$c0a0=vote$consistency1+vote$consistency2==0 & vote$agree1+vote$agree2==0
vote$c0a1=vote$consistency1+vote$consistency2==0 & vote$agree1+vote$agree2==1
vote$c0a2=vote$consistency1+vote$consistency2==0 & vote$agree1+vote$agree2==2

vote$c1a0=vote$consistency1+vote$consistency2==1 & vote$agree1+vote$agree2==0
vote$c1a2=vote$consistency1+vote$consistency2==1 & vote$agree1+vote$agree2==2

vote$c1a1=vote$consistency1+vote$consistency2==1 & vote$agree1+vote$agree2==1
## does agreement x constituency matter on the
# 1 0 | 0 1
# 0 1 | 1 0

# 1 0 | 1 0
# 0 1 | 0 1
vote$c1a1_align=(vote$consistency1==1 & vote$agree1==1 & vote$consistency2==0 & vote$agree2==0) | (vote$consistency1==0 & vote$agree1==0 & vote$consistency2==1 & vote$agree2==1)
vote$c1a1_misal=(vote$consistency1==0 & vote$agree1==1 & vote$consistency2==1 & vote$agree2==0) | (vote$consistency1==1 & vote$agree1==0 & vote$consistency2==0 & vote$agree2==1)

vote$c2a0=vote$consistency1+vote$consistency2==2 & vote$agree1+vote$agree2==0
vote$c2a1=vote$consistency1+vote$consistency2==2 & vote$agree1+vote$agree2==1
vote$c2a2=vote$consistency1+vote$consistency2==2 & vote$agree1+vote$agree2==2


attach(vote)
pred_prob_invote <- lm(dv_choice~
	#vote$c0a0+
	vote$c0a1+vote$c0a2+
	vote$c1a0+vote$c1a1+vote$c1a2+
	vote$c2a0+vote$c2a1+vote$c2a2,subset=which(vote$invoted=='invote'))
pred_prob_invote_clse <- clse.f(vote[which(vote$invoted=='invote'),],pred_prob_invote,respondent[which(vote$invoted=='invote')])


###
attach(vote)
# fixing 2 consistent positions :: co-party and out-party voters

##########################################
# in party
##########################################

weighted.mean(w=wt[which(pid3!=0 & invoted=='invote' & consistency1==1 & consistency2==1)],dv_choice[which(pid3!=0 & invoted=='invote' & consistency1==1 & consistency2==1)])
#0.570
weighted.mean(w=wt[which(pid3!=0 & invoted=='invote' & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))],dv_choice[which(pid3!=0 & invoted=='invote' & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))])
#0.443
weighted.mean(w=wt[which(pid3!=0 & invoted=='invote' & consistency1==0 & consistency2==0)],dv_choice[which(pid3!=0 & invoted=='invote' & consistency1==0 & consistency2==0)])
#0.304

# consistent positions drop off less among those who disagree w/n party
x1=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)],dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)])
n1=length(dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)])
#0.597
x2=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))],dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))])
n2=length(dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))])
#0.520
x3=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)],dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)])
n3=length(dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)])
#0.444

y1=sum((c(n1,n2,n3)/sum(c(n1,n2,n3)))*c(x1,x2,x3))


x7=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)],dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)])
n7=length(dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)])
#0.347
x8=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))],dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))])
n8=length(dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))])
#0.327
x9=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)],dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)])
n9=length(dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)])
#0.203

y3=sum((c(n7,n8,n9)/sum(c(n7,n8,n9)))*c(x7,x8,x9))


# happily split :: similar split/cross-pressure
x4=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))],dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))])
n4=length(dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))])
#0.461 #=> deviate from a consistent position in party primary, who expect to replace above loss on one deviation

# merge two-way split
# party consistent defectors
#mean(dv_choice[which(invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))])
#0.401 #=> deviate from a consistent position in party primary, who expect is first to go on one deviation

# moderate splits
#mean(dv_choice[which(invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))])
#0.434 #=> deviate from a consistent position in party primary, who expect is first to go on one deviation

x5=weighted.mean(w=c(wt[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],wt[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]),
c(dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]))
n5=length(c(dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]))

# unhappily split ::
x6=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))],dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))])
n6=length(dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))])
#0.369 #=> deviate from a consistent position in party invote, who expect to replace above loss on one deviation

y2=sum((c(n4,n5,n6)/sum(c(n4,n5,n6)))*c(x4,x5,x6))


defect_mat=rbind(c(x1,x2,x3),c(x4,x5,x6),c(x7,x8,x9))
rownames(defect_mat)=c('consistent 2','consistent 1','consistent 0')
colnames(defect_mat)=c('agree 2','agree 1','agree 0')
defect_mat=cbind(defect_mat,c(y1,y2,y3))

#effective unit sizes
defect_mat_n=rbind(
	c(
		round((n1/nrow(invote))*length(unique(invote$respondent))),
		round((n2/nrow(invote))*length(unique(invote$respondent))),
		round((n3/nrow(invote))*length(unique(invote$respondent)))
	),
	c(
		round((n4/nrow(invote))*length(unique(invote$respondent))),
		round((n5/nrow(invote))*length(unique(invote$respondent))),
		round((n6/nrow(invote))*length(unique(invote$respondent)))
	),
	c(
		round((n7/nrow(invote))*length(unique(invote$respondent))),
		round((n8/nrow(invote))*length(unique(invote$respondent))),
		round((n9/nrow(invote))*length(unique(invote$respondent)))
	)
)


rownames(defect_mat_n)=c('consistent 2','consistent 1','consistent 0')
colnames(defect_mat_n)=c('agree 2','agree 1','agree 0')
#defect_mat_n=cbind(defect_mat_n,c(y1,y2,y3))
## exploring the outparty conditions

yy1=c(y1,y2,y3)


##########################################
# out party
##########################################
# above is just co-partisans
# below merges out-partisans

#
weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & consistency1==1 & consistency2==1)],dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==1 & consistency2==1)])
#0.415
weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))],dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))])
#0.544
weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & consistency1==0 & consistency2==0)],dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==0 & consistency2==0)])
#0.654

# consistent positions drop off less among those who disagree w/n party
x1=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)],dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)])
n1=length(dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)])
#0.523
x2=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))],dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))])
n2=length(dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))])
#0.449
x3=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)],dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)])
n3=length(dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)])
#0.347

y1=sum((c(n1,n2,n3)/sum(c(n1,n2,n3)))*c(x1,x2,x3))

x7=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)],dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)])
n7=length(dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)])
#0.712
x8=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))],dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))])
n8=length(dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))])
#0.586
x9=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)],dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)])
n9=length(dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)])
#0.535

y3=sum((c(n7,n8,n9)/sum(c(n7,n8,n9)))*c(x7,x8,x9))


# happily split :: similar split/cross-pressure
x4=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))],dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))])
n4=length(dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))])
#0.543 #=> deviate from a consistent position in party invote, who expect to replace above loss on one deviation

# party consistent defectors
#mean(dv_choice[which(invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))])
#0.600 #=> deviate from a consistent position in party invote, who expect is first to go on one deviation

# moderate splits
#mean(dv_choice[which(invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))])
#0.546 #=> deviate from a consistent position in party invote, who expect is first to go on one deviation

x5=weighted.mean(w=c(wt[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],wt[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]),
c(dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]))
n5=length(c(dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]))

# unhappily split ::
x6=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))],dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))])
n6=length(dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))])
#0.498 #=> deviate from a consistent position in party invote, who expect to replace above loss on one deviation

y2=sum((c(n4,n5,n6)/sum(c(n4,n5,n6)))*c(x4,x5,x6))


cross_mat=rbind(c(x1,x2,x3),c(x4,x5,x6),c(x7,x8,x9))
rownames(cross_mat)=c('consistent 2','consistent 1','consistent 0')
colnames(cross_mat)=c('agree 2','agree 1','agree 0')
cross_mat=cbind(cross_mat,c(y1,y2,y3))

#effective unit sizes
cross_mat_n=rbind(
	c(
		round((n1/nrow(outvote))*length(unique(outvote$respondent))),
		round((n2/nrow(outvote))*length(unique(outvote$respondent))),
		round((n3/nrow(outvote))*length(unique(outvote$respondent)))
	),
	c(
		round((n4/nrow(outvote))*length(unique(outvote$respondent))),
		round((n5/nrow(outvote))*length(unique(outvote$respondent))),
		round((n6/nrow(outvote))*length(unique(outvote$respondent)))
	),
	c(
		round((n7/nrow(outvote))*length(unique(outvote$respondent))),
		round((n8/nrow(outvote))*length(unique(outvote$respondent))),
		round((n9/nrow(outvote))*length(unique(outvote$respondent)))
	)
)


rownames(cross_mat_n)=c('consistent 2','consistent 1','consistent 0')
colnames(cross_mat_n)=c('agree 2','agree 1','agree 0')

yy2=c(y1,y2,y3)


#
weighted.mean(w=wt[which(pid3==0 & invoted=='invote' & consistency1==1 & consistency2==1 | pid3==0 & invoted=='outvote' & consistency1==1 & consistency2==1)],dv_choice[which(pid3==0 & invoted=='invote' & consistency1==1 & consistency2==1 | pid3==0 & invoted=='outvote' & consistency1==1 & consistency2==1)])
#0.415
weighted.mean(w=wt[which(pid3==0 & invoted=='invote' & (consistency1==1 & consistency2==0 | pid3==0 & consistency1==0 & consistency2==1) | invoted=='outvote' & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))],dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & consistency2==0 | pid3==0 & consistency1==0 & consistency2==1) | invoted=='outvote' & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))])
#0.544
weighted.mean(w=wt[which(pid3==0 & invoted=='invote' & consistency1==0 & consistency2==0 | pid3==0 & invoted=='outvote' & consistency1==0 & consistency2==0)],dv_choice[which(pid3==0 & invoted=='invote' & consistency1==0 & consistency2==0 | pid3==0 & invoted=='outvote' & consistency1==0 & consistency2==0)])
#0.654

# consistent positions drop off less among those who disagree w/n party
x1=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)],wt[which(pid3==0 & invoted=='outvote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)]),
c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)],
  dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)]))
n1=length(c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)],dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)]))
#0.523
x2=weighted.mean(w=
	c(wt[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))],
		wt[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))]), #consistency2==1 & agree2==1))]),
	c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))],
  	dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))])) #consistency2==1 & agree2==1))]))
n2=length(c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))],dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))]))
#0.449
x3=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)],wt[which(pid3==0 & invoted=='outvote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)]),
	c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)],
	  dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)]))
n3=length(c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)],dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)]))
#0.347

y1=sum((c(n1,n2,n3)/sum(c(n1,n2,n3)))*c(x1,x2,x3))

x7=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)],wt[which(pid3==0 & invoted=='outvote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)]),
	c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)],
	  dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)]))
n7=length(c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)],dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)]))
#0.712
x8=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))],wt[which(pid3==0 & invoted=='outvote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))]),
	c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))],
	  dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))]))
n8=length(c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))],dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))]))
#0.586
x9=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)],wt[which(pid3==0 & invoted=='outvote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)]),
	c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)],
	  dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)]))
n9=length(c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)],dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)]))
#0.535

y3=sum((c(n7,n8,n9)/sum(c(n7,n8,n9)))*c(x7,x8,x9))

# happily split :: similar split/cross-pressure
x4=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))],wt[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))]),
	c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))],
	  dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))]))
n4=length(c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))],dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))]))
#0.543 #=> deviate from a consistent position in party invote, who expect to replace above loss on one deviation

# party consistent defectors
#mean(dv_choice[which(invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))])
#0.600 #=> deviate from a consistent position in party invote, who expect is first to go on one deviation

# moderate splits
#mean(dv_choice[which(invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))])
#0.546 #=> deviate from a consistent position in party invote, who expect is first to go on one deviation

x5=weighted.mean(w=
	c(wt[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
	wt[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))],
	wt[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
	wt[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]),
				c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
					dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))],
					dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
					dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]))
n5=length(
				c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
					dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))],
					dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
					dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]))
#

# unhappily split ::
x6=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))],wt[which(pid3==0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))]),
				c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))],
					dv_choice[which(pid3==0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))]))
n6=length(c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))],dv_choice[which(pid3==0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))]))
#0.498 #=> deviate from a consistent position in party invote, who expect to replace above loss on one deviation

y2=sum((c(n4,n5,n6)/sum(c(n4,n5,n6)))*c(x4,x5,x6))


indep_mat=rbind(c(x1,x2,x3),c(x4,x5,x6),c(x7,x8,x9))
rownames(indep_mat)=c('consistent 2','consistent 1','consistent 0')
colnames(indep_mat)=c('agree 2','agree 1','agree 0')
indep_mat=cbind(indep_mat,c(y1,y2,y3))

yy3=c(y1,y2,y3)

p1=1
p2=0
p3=0
#2 to 1; out to ind
pt=1e+09
while((yy1*p1+yy2*p2+yy3*p3)[1]>(yy1*p1+yy2*p2+yy3*p3)[3]){
	p1=p1-.01
	p2=(1-p1)*pt/(pt+1)
	p3=(1-p1)*1/(pt+1)
}

p1
#0.46
p2
#0.54
p3
#0

# brief CQ exploration is not really
# by CQ?
weighted.mean(w=wt[which(invoted=='invote' & cq==1 & consistency1==1 & consistency2==1)],dv_choice[which(invoted=='invote' & cq==1 & consistency1==1 & consistency2==1)])
weighted.mean(w=wt[which(invoted=='invote' & cq>1 & cq<4 & consistency1==1 & consistency2==1)],dv_choice[which(invoted=='invote' & cq>1 & cq<4 & consistency1==1 & consistency2==1)])
#0.563

weighted.mean(w=wt[which(invoted=='invote' & cq==1 & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))],dv_choice[which(invoted=='invote' & cq==1 & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))])
weighted.mean(w=wt[which(invoted=='invote' & cq>1 & cq<4 & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))],dv_choice[which(invoted=='invote' & cq>1 & cq<4 & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))])

#0.420
weighted.mean(w=wt[which(invoted=='invote' & cq==1 & consistency1==0 & consistency2==0)],dv_choice[which(invoted=='invote' & cq==1 & consistency1==0 & consistency2==0)])
weighted.mean(w=wt[which(invoted=='invote' & cq>1 & cq<4 & consistency1==0 & consistency2==0)],dv_choice[which(invoted=='invote' & cq>1 & cq<4 & consistency1==0 & consistency2==0)])
#0.285


#replicate using a aggregate measure of cross-pressure,
# a) above agreement measures could have error which distorts measures of support
# b) use summary score, i.e., the average of the libcon measures to decide 'cross-pressure'
#  - set threshold at i) 25% and ii) 50% of party supporters

x1=weighted.mean(w=wt[which(invoted=='invote' & consistency==2 & crossp25==1)],dv_choice[which(invoted=='invote' & consistency==2 & crossp25==1)])
n1=length(dv_choice[which(invoted=='invote' & consistency==2 & crossp25==1)])
#0.5100464
x2=weighted.mean(w=wt[which(invoted=='invote' & consistency==2 & crossp25==0)],dv_choice[which(invoted=='invote' & consistency==2 & crossp25==0)])
n2=length(dv_choice[which(invoted=='invote' & consistency==2 & crossp25==0)])
#0.5808467

y1=sum((c(n1,n2)/sum(c(n1,n2)))*c(x1,x2))

x3=weighted.mean(w=wt[which(invoted=='invote' & consistency==1 & crossp25==1)],dv_choice[which(invoted=='invote' & consistency==1 & crossp25==1)])
n3=length(dv_choice[which(invoted=='invote' & consistency==1 & crossp25==1)])
#0.4745342
x4=weighted.mean(w=wt[which(invoted=='invote' & consistency==1 & crossp25==0)],dv_choice[which(invoted=='invote' & consistency==1 & crossp25==0)])
n4=length(dv_choice[which(invoted=='invote' & consistency==1 & crossp25==0)])
#0.4025267

y2=sum((c(n3,n4)/sum(c(n3,n4)))*c(x3,x4))

x5=weighted.mean(w=wt[which(invoted=='invote' & consistency==0 & crossp25==1)],dv_choice[which(invoted=='invote' & consistency==0 & crossp25==1)])
n5=length(dv_choice[which(invoted=='invote' & consistency==0 & crossp25==1)])
#0.4633028
x6=weighted.mean(w=wt[which(invoted=='invote' & consistency==0 & crossp25==0)],dv_choice[which(invoted=='invote' & consistency==0 & crossp25==0)])
n6=length(dv_choice[which(invoted=='invote' & consistency==0 & crossp25==0)])
# 0.2339869

y3=sum((c(n5,n6)/sum(c(n5,n6)))*c(x5,x6))


cp25_mat=rbind(c(x2,x1),c(x4,x3),c(x6,x5))
rownames(cp25_mat)=c('consistent 2','consistent 1','consistent 0')
colnames(cp25_mat)=c('not cross','cross')
cp25_mat=cbind(cp25_mat,c(y1,y2,y3))

yy1=c(y1,y2,y3)



x1=weighted.mean(w=wt[which(invoted=='invote' & consistency==2 & crossp50==1)],dv_choice[which(invoted=='invote' & consistency==2 & crossp50==1)])
n1=length(dv_choice[which(invoted=='invote' & consistency==2 & crossp50==1)])
#0.5255489
x2=weighted.mean(w=wt[which(invoted=='invote' & consistency==2 & crossp50==0)],dv_choice[which(invoted=='invote' & consistency==2 & crossp50==0)])
n2=length(dv_choice[which(invoted=='invote' & consistency==2 & crossp50==0)])
#0.6000379

y1=sum((c(n1,n2)/sum(c(n1,n2)))*c(x1,x2))

x3=weighted.mean(w=wt[which(invoted=='invote' & consistency==1 & crossp50==1)],dv_choice[which(invoted=='invote' & consistency==1 & crossp50==1)])
n3=length(dv_choice[which(invoted=='invote' & consistency==1 & crossp50==1)])
#0.4633999
x4=weighted.mean(w=wt[which(invoted=='invote' & consistency==1 & crossp50==0)],dv_choice[which(invoted=='invote' & consistency==1 & crossp50==0)])
n4=length(dv_choice[which(invoted=='invote' & consistency==1 & crossp50==0)])
#0.3777907

y2=sum((c(n3,n4)/sum(c(n3,n4)))*c(x3,x4))

x5=weighted.mean(w=wt[which(invoted=='invote' & consistency==0 & crossp50==1)],dv_choice[which(invoted=='invote' & consistency==0 & crossp50==1)])
n5=length(dv_choice[which(invoted=='invote' & consistency==0 & crossp50==1)])
#0.3604888
x6=weighted.mean(w=wt[which(invoted=='invote' & consistency==0 & crossp50==0)],dv_choice[which(invoted=='invote' & consistency==0 & crossp50==0)])
n6=length(dv_choice[which(invoted=='invote' & consistency==0 & crossp50==0)])
#0.2093496

y3=sum((c(n5,n6)/sum(c(n5,n6)))*c(x5,x6))


cp50_mat=rbind(c(x2,x1),c(x4,x3),c(x6,x5))
rownames(cp50_mat)=c('consistent 2','consistent 1','consistent 0')
colnames(cp50_mat)=c('not cross','cross')
cp50_mat=cbind(cp50_mat,c(y1,y2,y3))

yy2=c(y1,y2,y3)

print(xtable(defect_mat,digits=3),file='appendix/figures/inparty_defect_L.tex')
#> defect_mat

print(xtable(cross_mat,digits=3),file='appendix/figures/outparty_defect_L.tex')
#> cross_mat

# END
